import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JButton;

import com.thoughtworks.xstream.XStream;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;


public class mainMenu extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					mainMenu frame = new mainMenu();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public mainMenu() {
		setTitle("Welcome To Boggle");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 423, 170);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		setLocationRelativeTo(null);

		JLabel lblBoggle = new JLabel("Boggle");
		lblBoggle.setHorizontalAlignment(SwingConstants.CENTER);
		lblBoggle.setFont(new Font("Tahoma", Font.PLAIN, 80));
		lblBoggle.setBounds(10, 0, 268, 122);
		contentPane.add(lblBoggle);

		//New Game
		JButton btnNewGame = new JButton("New Game");
		btnNewGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new newGame().setVisible(true);
				dispose();
			}
		});
		btnNewGame.setBounds(288, 27, 124, 23);
		contentPane.add(btnNewGame);

		//Load Game
		JButton btnLoadGame = new JButton("Load Game");
		btnLoadGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					File savedGameFile;
					JFileChooser chooser = new JFileChooser();
					chooser.setFileFilter(new FileNameExtensionFilter("XML Files", "xml"));
					if(chooser.showOpenDialog(getParent()) == JFileChooser.APPROVE_OPTION){
						savedGameFile = chooser.getSelectedFile();
						XStream xstream = new XStream();
						boggleGame newGame = (boggleGame)xstream.fromXML(savedGameFile);
						boggleMainGame newBoggleGame = new boggleMainGame(newGame);
						dispose();
					}
				}catch (Exception e1){
					JOptionPane.showMessageDialog(getParent(),"Error loading the game");
				}
			}
		});
		btnLoadGame.setBounds(288, 61, 124, 23);
		contentPane.add(btnLoadGame);

		JButton btnQuit = new JButton("Quit");
		btnQuit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnQuit.setBounds(288, 95, 124, 23);
		contentPane.add(btnQuit);
	}
}
